module.exports = app => {
  const express = require('express')
  const Specialty = require('../../models/Specialty')
  const Department = require(`../../models/Department`)
  const User = require('../../models/User')
  const Class = require('../../models/Class')
  const auth=require("../../middleware/auth")
  /*------------------多次插入的公共接口------------------------------*/
  const irouter = express.Router({
    mergeParams: true
  })
  irouter.post('/', function (req, res) {
    req.Model.find().exec(async (err, doc) => {
      if (err) {
        return res.json({
          success: false,
          message: err
        })
      }
      //将一段长的数组，进行json转换
      let isContinue = true;
      if (req.Model.modelName == "Teacher" || req.Model.modelName == "Student") {
        let jsDoc = JSON.stringify(doc)
        for (let i = 0; i < req.body.length; i++) {
          if (jsDoc.indexOf(req.body[i].nid) != -1 || jsDoc.indexOf(req.body[i].identity) != -1) {
            //然后在这个json字符串中查找对应的字段
            isContinue = false;
            return res.json({
              success: false,
              message: `第${i+1}行信息已经存在，请勿多次添加！`
            })
          }
          if (req.Model.modelName == "Student") {
            let classRes = await Class.findById(req.body[i].class)
            req.body[i].class = classRes._id
            let specialtyRes = await Specialty.findById(req.body[i].specialty)
            req.body[i].specialty = specialtyRes._id;
          } else if (req.Model.modelName == "Teacher") {
            let departmentRes = await Department.findById(req.body[i].department)
            req.body[i].department = departmentRes._id;
          }
        }

      }
      if (req.Model.modelName == "Class") {
        let jsDoc = JSON.stringify(doc)
        for (let i = 0; i < req.body.length; i++) {
          if (jsDoc.indexOf(req.body[i].nid) != -1 || jsDoc.indexOf(req.body[i].name) != -1) {
            //然后在这个json字符串中查找对应的字段
            isContinue = false;
            return res.json({
              success: false,
              message: `第${i+1}行信息已经存在，请勿多次添加！`
            })
          }
          let superiorRes = await Specialty.findById(req.body[i].superior)
          req.body[i].superior = superiorRes._id
        }
      }

      if (isContinue) {
        req.Model.collection.insert(req.body, (err, doc) => {
          if (err) {
            res.send({
              success: false,
              message: err
            })
          }
          //只有老师和教师的批量插入，才有用户的增加
          if (req.Model.modelName == "Teacher" || req.Model.modelName == "Student") {
            let userList = []
            let identity = "0";
            if (req.Model.modelName == "Teacher") {
              identity = "1";
            }
            for (let i = 0; i < doc.ops.length; i++) {
              let useInfo = {
                username: doc.ops[i].nid,
                bindId: doc.ops[i]._id,
                identity: identity,
                password: "123456",
              }
              userList.push(useInfo)
            }
            User.collection.insert(userList, (err, doc) => {
              if (err) {
                res.send({
                  success: false,
                  message: err
                })
              }
              res.send({
                success: true,
                message: '数据导入成功！'
              })
            })
          } else {
            res.send({
              success: true,
              message: '数据导入成功！'
            })
          }
        })
      }
    })
  })
  app.use('/admin/api/insertRest/insertMore/:resource',auth(), async (req, res, next) => {
    try {
      req.Model = require(`../../models/${req.params.resource}`)
      next()
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  }, irouter)
}